void HeapSort(int* a, int n)
{
	for (int i = 0;i < n;i++)
	{
		AdjustUp(a, i);
		int end = n - 1;
	}
	while (end > 0)
	{
		swap(&a[0], &a[end]);
		Ajustdown(a, end, 0);
		--end;
	}
}

void swap(int* p1, int* p2)
{
	HPDataType emp;
	emp = p1;
	p1 = p2;
	p2 = emp;
}

void AdjustUp(HPDataType* a,int child)
{
	int parent = (child - 1) / 2;
	
	if (a[child] < a[parent])
	{
		swap(&a[child], &a[parent]);
		child = parent;
		parent = (parent - 1) / 2;
	}
	else
	{
		break;
	}
}

void Ajustdown(int* a, int size, int parent)
{
	int child = parent * 2 + 1;
	
	while (child < size)
	{
		if (child + 1 < size && a[child] < a[child + 1])
		{
			child++;
		}
		if (a[child] < a[parent])
		{
			swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}